// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.inventory;

import java.util.ArrayList;
import zombie.audio.BaseSoundEmitter;
import zombie.iso.IsoWorld;

public final class ItemSoundManager {
    private static final ArrayList<InventoryItem> items = new ArrayList();
    private static final ArrayList<BaseSoundEmitter> emitters = new ArrayList();
    private static final ArrayList<InventoryItem> toAdd = new ArrayList();
    private static final ArrayList<InventoryItem> toRemove = new ArrayList();
    private static final ArrayList<InventoryItem> toStopItems = new ArrayList();
    private static final ArrayList<BaseSoundEmitter> toStopEmitters = new ArrayList();

    public static void addItem(InventoryItem item) {
        if (item != null && !items.contains(item)) {
            toRemove.remove(item);
            int _int = toStopItems.indexOf(item);
            if (_int != -1) {
                toStopItems.remove(_int);
                BaseSoundEmitter baseSoundEmitter = (BaseSoundEmitter)toStopEmitters.remove(_int);
                items.add(item);
                emitters.add(baseSoundEmitter);
            } else if (!toAdd.contains(item)) {
                toAdd.add(item);
            }
        }
    }

    public static void removeItem(InventoryItem item) {
        toAdd.remove(item);
        int _int = items.indexOf(item);
        if (item != null && _int != -1) {
            if (!toRemove.contains(item)) {
                toRemove.add(item);
            }
        }
    }

    public static void removeItems(ArrayList<InventoryItem> _items) {
        for (int _int = 0; _int < _items.size(); _int++) {
            removeItem((InventoryItem)_items.get(_int));
        }
    }

    public static void update() {
        if (!toStopItems.isEmpty()) {
            for (int int0 = 0; int0 < toStopItems.size(); int0++) {
                BaseSoundEmitter baseSoundEmitter0 = (BaseSoundEmitter)toStopEmitters.get(int0);
                baseSoundEmitter0.stopAll();
                IsoWorld.instance.returnOwnershipOfEmitter(baseSoundEmitter0);
            }

            toStopItems.clear();
            toStopEmitters.clear();
        }

        if (!toAdd.isEmpty()) {
            for (int int1 = 0; int1 < toAdd.size(); int1++) {
                InventoryItem inventoryItem0 = (InventoryItem)toAdd.get(int1);

                assert !items.contains(inventoryItem0);

                items.add(inventoryItem0);
                BaseSoundEmitter baseSoundEmitter1 = IsoWorld.instance.getFreeEmitter();
                IsoWorld.instance.takeOwnershipOfEmitter(baseSoundEmitter1);
                emitters.add(baseSoundEmitter1);
            }

            toAdd.clear();
        }

        if (!toRemove.isEmpty()) {
            for (int int2 = 0; int2 < toRemove.size(); int2++) {
                InventoryItem inventoryItem1 = (InventoryItem)toRemove.get(int2);

                assert items.contains(inventoryItem1);

                int int3 = items.indexOf(inventoryItem1);
                items.remove(int3);
                BaseSoundEmitter baseSoundEmitter2 = (BaseSoundEmitter)emitters.get(int3);
                emitters.remove(int3);
                toStopItems.add(inventoryItem1);
                toStopEmitters.add(baseSoundEmitter2);
            }

            toRemove.clear();
        }

        for (int int4 = 0; int4 < items.size(); int4++) {
            InventoryItem inventoryItem2 = (InventoryItem)items.get(int4);
            BaseSoundEmitter baseSoundEmitter3 = (BaseSoundEmitter)emitters.get(int4);
            ItemContainer itemContainer = inventoryItem2.getOutermostContainer();
            if (itemContainer != null) {
                if (itemContainer.containingItem != null && itemContainer.containingItem.getWorldItem() != null) {
                    if (itemContainer.containingItem.getWorldItem().getWorldObjectIndex() == -1) {
                        itemContainer = null;
                    }
                } else if (itemContainer.parent != null) {
                    if (itemContainer.parent.getObjectIndex() == -1
                        && itemContainer.parent.getMovingObjectIndex() == -1
                        && itemContainer.parent.getStaticMovingObjectIndex() == -1) {
                        itemContainer = null;
                    }
                } else {
                    itemContainer = null;
                }
            }

            if (itemContainer != null || inventoryItem2.getWorldItem() != null && inventoryItem2.getWorldItem().getWorldObjectIndex() != -1) {
                inventoryItem2.updateSound(baseSoundEmitter3);
                baseSoundEmitter3.tick();
            } else {
                removeItem(inventoryItem2);
            }
        }
    }

    public static void Reset() {
        items.clear();
        emitters.clear();
        toAdd.clear();
        toRemove.clear();
        toStopItems.clear();
        toStopEmitters.clear();
    }
}
